package game

import (
	"fmt"
	"qm_system_server/server/global"
	"qm_system_server/server/model/common/response"
	softPackage "qm_system_server/server/proto/grpc/package"

	"context"
	"strconv"

	"github.com/gin-gonic/gin"
)

type QmPackageApi struct {
}

// CreateQmPackage 创建游戏包
// @Tags QmPackage
// @Summary 创建游戏包
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body game.QmPackage true "创建游戏包"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /qmPackage/createQmPackage [post]
func (qmPackageApi *QmPackageApi) CreateQmPackage(c *gin.Context) {
	type request struct {
		PackageName string `json:"package_name" form:"package_name"`
		GameId      int64  `json:"game_id" form:"game_id"`
		SoftId      int64  `json:"soft_id" form:"soft_id"`
		IsRecord    int64  `json:"is_record" form:"is_record"`     // 是否备案
		PlatformId  int64  `json:"platform_id" form:"platform_id"` // 平台id
	}

	var req request
	_ = c.ShouldBindJSON(&req)
	fmt.Println(req)
	if req.GameId == 0 || req.PackageName == "" || req.SoftId == 0 || req.PlatformId == 0 || req.IsRecord == 0 {
		response.FailWithMessage("参数不能为空", c)
		return
	}

	client := softPackage.NewPackageServiceClient(global.ServerGrpc)
	_, err := client.Create(context.Background(), &softPackage.CreateRequest{
		Package: &softPackage.Package{
			PackageName: req.PackageName,
			GameId:      req.GameId,
			SoftId:      req.SoftId,
			IsRecord:    req.IsRecord,
			PlatformId:  req.PlatformId,
		},
	})
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteQmPackage 删除游戏包
// @Tags QmPackage
// @Summary 删除游戏包
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body game.QmPackage true "删除游戏包"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /qmPackage/deleteQmPackage [delete]
func (qmPackageApi *QmPackageApi) DeleteQmPackage(c *gin.Context) {
	id := c.Query("id")

	if id == "" {
		response.FailWithMessage("参数不能为空", c)
		return
	}

	idInt, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		response.FailWithMessage("参数不能为空", c)
		return
	}

	client := softPackage.NewPackageServiceClient(global.ServerGrpc)

	_, err = client.Delete(context.Background(), &softPackage.DeleteRequest{
		Id: idInt,
	})

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// UpdateQmPackage 更新游戏包
// @Tags QmPackage
// @Summary 更新游戏包
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body game.QmPackage true "更新游戏包"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /qmPackage/updateQmPackage [put]
func (qmPackageApi *QmPackageApi) UpdateQmPackage(c *gin.Context) {
	type request struct {
		Id          int64  `json:"id" form:"id"`
		PackageName string `json:"package_name" form:"package_name"`
		GameId      int64  `json:"game_id" form:"game_id"`
		SoftId      int64  `json:"soft_id" form:"soft_id"`
		IsRecord    int64  `json:"is_record" form:"is_record"`     // 是否备案
		PlatformId  int64  `json:"platform_id" form:"platform_id"` // 平台id
	}

	var req request
	_ = c.ShouldBindJSON(&req)

	if req.Id == 0 || req.GameId == 0 || req.PackageName == "" || req.SoftId == 0 || req.PlatformId == 0 || req.IsRecord == 0 {
		response.FailWithMessage("参数不能为空", c)
		return
	}

	client := softPackage.NewPackageServiceClient(global.ServerGrpc)

	_, err := client.Update(context.Background(), &softPackage.UpdateRequest{
		Package: &softPackage.Package{
			Id:          req.Id,
			PackageName: req.PackageName,
			GameId:      req.GameId,
			SoftId:      req.SoftId,
			IsRecord:    req.IsRecord,
			PlatformId:  req.PlatformId,
		},
	})

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindQmPackage 用id查询游戏包
// @Tags QmPackage
// @Summary 用id查询游戏包
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query game.QmPackage true "用id查询游戏包"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /qmPackage/findQmPackage [get]
func (qmPackageApi *QmPackageApi) FindQmPackage(c *gin.Context) {
	id := c.Query("id")

	if id == "" {
		response.FailWithMessage("参数不能为空", c)
		return
	}

	idInt, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		response.FailWithMessage("参数不能为空", c)
		return
	}

	client := softPackage.NewPackageServiceClient(global.ServerGrpc)

	getResp, err := client.Get(context.Background(), &softPackage.GetRequest{
		Id: idInt,
	})

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithData(getResp.Package, c)
}

// GetQmPackageList 分页获取游戏包列表
// @Tags QmPackage
// @Summary 分页获取游戏包列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query game.QmPackageSearch true "分页获取游戏包列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /qmPackage/getQmPackageList [get]
func (qmPackageApi *QmPackageApi) GetQmPackageList(c *gin.Context) {
	type request struct {
		Page        int64  `json:"page" form:"page"`
		PageSize    int64  `json:"page_size" form:"page_size"`
		PackageName string `json:"package_name" form:"package_name"`
		GameId      int64  `json:"game_id" form:"game_id"`
		DevelopType int64  `json:"develop_type" form:"develop_type"`
		CpName      string `json:"cp_name" form:"cp_name"`
		SoftId      int64  `json:"soft_id" form:"soft_id"`
		OriginName  string `json:"origin_name" form:"origin_name"`
		IsRecord    int64  `json:"is_record" form:"is_record"`     // 是否备案
		PlatformId  int64  `json:"platform_id" form:"platform_id"` // 平台id
		Type        int64  `json:"type" form:"type"`               // 类型
		Main        string `json:"main" form:"main"`               // 主体
	}

	var req request
	_ = c.ShouldBindQuery(&req)

	client := softPackage.NewPackageServiceClient(global.ServerGrpc)

	packages, err := client.List(context.Background(), &softPackage.ListRequest{
		Page:        req.Page,
		PackageName: req.PackageName,
		GameId:      req.GameId,
		DevelopType: req.DevelopType,
		CpName:      req.CpName,
		SoftId:      req.SoftId,
		OriginName:  req.OriginName,
		IsRecord:    req.IsRecord,
		PlatformId:  req.PlatformId,
		Type:        req.Type,
		Main:        req.Main,
	})

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithData(response.PageResult{
		List:  packages.Package,
		Total: packages.Total,
		Page:  int(req.Page),
	}, c)
}
